Inside Macintosh: Files

Previous | Chapter Top | Chapter Contents | Next

Opening, Creating, and Deleting Files

The File Manager provides the FSpOpenDF , FSpCreate , and FSpDelete routines, which allow you to open, create, and delete files.

FSpOpenDF

You can use the FSpOpenDF function to open a file's data fork.

FUNCTION FSpOpenDF (spec: FSSpec; permission: SignedByte;
                                         VAR refNum: Integer): OSErr;
spec
An FSSpec record specifying the file whose data fork is to be opened.
permission
A constant indicating the desired file access permissions.
refNum
A reference number of an access path to the file's data fork.

DESCRIPTION

The FSpOpenDF function opens the data fork of the file specified by the spec parameter and returns a file reference number in the refNum parameter. You can pass that reference number as a parameter to any of the low- or high-level file access routines.

The permission parameter specifies the kind of access permission mode you want. You can specify one of these constants:

CONST
    fsCurPerm           =   0;      {whatever permission is allowed}
    fsRdPerm            =   1;      {read permission}
    fsWrPerm            =   2;      {write permission}
    fsRdWrPerm          =   3;      {exclusive read/write permission}
    fsRdWrShPerm        =   4;      {shared read/write permission}

In most cases, you can simply set the permission parameter to fsCurPerm . Some applications request fsRdWrPerm , to ensure that they can both read from and write to a file.

RESULT CODES

noErr

0

No error

nsvErr

-35

No such volume

ioErr

-36

I/O error

bdNamErr

-37

Bad filename

tmfoErr

-42

Too many files open

fnfErr

-43

File not found

opWrErr

-49

File already open for writing

permErr

-54

Attempt to open locked file for writing

dirNFErr

-120

Directory not found or incomplete pathname

afpAccessDenied

-5000

User does not have the correct access to the file

FSpCreate

You can use the FSpCreate function to create a new file.

FUNCTION FSpCreate (spec: FSSpec; creator: OSType;
                                         fileType: OSType; scriptTag: ScriptCode):
                                         OSErr;
spec
An FSSpec record specifying the file to be created.
creator
The creator of the new file.
fileType
The file type of the new file.
scriptTag
The code of the script system in which the filename is to be displayed. If you have established the name and location of the new file using either the StandardPutFile or CustomPutFile procedure, specify the script code returned in the reply record. (See the chapter "Standard File Package" in this book for a description of StandardPutFile and CustomPutFile .) Otherwise, specify the system script by setting the scriptTag parameter to the value smSystemScript .

DESCRIPTION

The FSpCreate function creates a new file (both forks) with the specified type, creator, and script code. The new file is unlocked and empty. The date and time of creation and last modification are set to the current date and time.

See the chapter "Finder Interface" in Inside Macintosh: Macintosh Toolbox Essentials for information on file types and creators.

Files created using FSpCreate are not automatically opened. If you want to write data to the new file, you must first open the file using a file access routine (such as FSpOpenDF).

The resource fork of the new file exists but is empty. You'll need to call one of the Resource Manager procedures CreateResFile , HCreateResFile , or FSpCreateResFile to create a resource map in the file before you can open it (by calling one of the Resource Manager functions OpenResFile , HOpenResFile , or FSpOpenResFile ).

RESULT CODES

noErr

0

No error

dirFulErr

-33

File directory full

dskFulErr

-34

Disk is full

nsvErr

-35

No such volume

ioErr

-36

I/O error

bdNamErr

-37

Bad filename

fnfErr

-43

Directory not found or incomplete pathname

wPrErr

-44

Hardware volume lock

vLckdErr

-46

Software volume lock

dupFNErr

-48

Duplicate filename and version

dirNFErr

-120

Directory not found or incomplete pathname

afpAccessDenied

-5000

User does not have the correct access

afpObjectTypeErr

-5025

A directory exists with that name

FSpDelete

You can use the FSpDelete function to delete files and directories.

FUNCTION FSpDelete (spec: FSSpec): OSErr;
spec
An FSSpec record specifying the file or directory to delete.

DESCRIPTION

The FSpDelete function removes a file or directory. If the specified target is a file, both forks of the file are deleted. The file ID reference, if any, is removed.

A file must be closed before you can delete it. Similarly, a directory must be empty before you can delete it. If you attempt to delete an open file or a nonempty directory, FSpDelete returns the result code fBsyErr . FSpDelete also returns the result code fBsyErr if the directory has an open working directory associated with it.

RESULT CODES

noErr

0

No error

nsvErr

-35

No such volume

ioErr

-36

I/O error

bdNamErr

-37

Bad filename

fnfErr

-43

File not found

wPrErr

-44

Hardware volume lock

fLckdErr

-45

File is locked

vLckdErr

-46

Software volume lock

fBsyErr

-47

File busy, directory not empty, or working directory control block open

dirNFErr

-120

Directory not found or incomplete pathname

afpAccessDenied

-5000

User does not have the correct access


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next